package com.chaoyong.higo.adapter;

import com.chaoyong.higo.R;
import com.chaoyong.higo.activity.LogisticsInfoActivity;
import com.chaoyong.higo.activity.ProductDetailActivity;
import com.chaoyong.higo.activity.SharDetailActivity;
import com.chaoyong.higo.activity.StartActivity;
import com.chaoyong.higo.activity.ToShowOrderActivity;
import com.chaoyong.higo.base.BaseAdapter;
import com.chaoyong.higo.bean.GetAllGoodsBean_z;
import com.chaoyong.higo.bean.GetgoodsAllBean;
import com.chaoyong.higo.bean.ListEntity;
import com.chaoyong.higo.bean.ShowOrderEntity;
import com.chaoyong.higo.utils.DataTimeTools;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.utils.ViewHolder;
import com.lidroid.xutils.BitmapUtils;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 获得商品
 * 
 * @author zk
 *
 */
public class GetGoodsAdapter extends BaseAdapter<GetgoodsAllBean.DataEntity.ListEntity> {

	/** 商品图片 */
	private ImageView get_goods_iv;
	/** 商品描述 */
	private TextView get_goods_tv;
	/** 商品价格 */
	private TextView get_price_tv;
	/** 揭晓时间 */
	private TextView get_konw_times;
	/** 订单号 */
	private TextView get_orderId_tv;
	/** 我要晒单 */
	private TextView get_show_orderInfo;
	/** 查看物流 */
	private TextView get_logistics_info;

	private LinearLayout get_part_in_lay;
	private LinearLayout item_get_goods_lay;

	public GetGoodsAdapter(Context context) {
		super(context);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_get_goods, null);
		}

		findview(convertView);

		// 商品名称
		String goods_name = mList.get(position).getGoods_name();
		// 商品描述
		String goods_expect = mList.get(position).getGoods_expect();
		// 商品价格
		String goods_price = mList.get(position).getGoods_price();
		// 揭晓时间
		String lottery_time = mList.get(position).getLottery_time();
		// 快递单号
		String kd_code = mList.get(position).getKd_code();
		
		final String shares = mList.get(position).getShares();

		if (shares.equals("y")) {
			//get_part_in_lay.setVisibility(View.INVISIBLE);
			get_show_orderInfo.setText("查看晒单");
		} else if(shares.equals("n")){
			//get_part_in_lay.setVisibility(View.VISIBLE);
			get_show_orderInfo.setText("我要晒单");
		}

		get_goods_tv.setText(goods_expect);
		get_price_tv.setText(goods_price);
		get_konw_times.setText(DataTimeTools.time2(lottery_time));
		get_orderId_tv.setText(kd_code);
		BitmapUtils utils = new BitmapUtils(mContext);
		final String uri = Values.BASE_IMAGE_URL + mList.get(position).getCover_img().get(0);
		utils.display(get_goods_iv, uri);

		// 我要晒单
		get_show_orderInfo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//已经晒过单了点击后查看晒单详情
				if (shares.equals("y")) {
					ShowOrderEntity entity = new ShowOrderEntity();
					entity.setCover_img(mList.get(position).getCover_img().get(0));
					entity.setExpect(mList.get(position).getExpect());
					entity.setGoods_expect(mList.get(position).getGoods_expect());
					entity.setGoods_id(mList.get(position).getGoods_id());
					entity.setGoods_name(mList.get(position).getGoods_name());
					entity.setGoods_price(mList.get(position).getGoods_price());
					entity.setId(mList.get(position).getId());
					entity.setShare_info(mList.get(position).getShare_info());
					entity.setShare_time(mList.get(position).getShare_time());
					entity.setShare_title(mList.get(position).getShare_title());
					entity.setUser_name(PrefUtils.getString(mContext, "dear_name", "岳云鹏"));
					entity.setUid_portrait(mList.get(position).getCover_img().get(0));
					entity.setRecord(mList.get(position).getBuy_num());
					Intent in = new Intent(mContext, SharDetailActivity.class);
					in.putExtra("sharEntity", entity);
					mContext.startActivity(in);
					//Toast.makeText(mContext, "已经晒过单了", 0).show();
					//没有晒过单才跳到晒单页面
				} else if(shares.equals("n")){
					Intent intent = new Intent(mContext, ToShowOrderActivity.class);
					intent.putExtra("goods_id", mList.get(position).getGoods_id());
					intent.putExtra("expect", mList.get(position).getExpect());
					intent.putExtra("img", mList.get(position).getCover_img().get(0));
					intent.putExtra("goods_name", mList.get(position).getGoods_name());
					intent.putExtra("goods_price", mList.get(position).getGoods_price());
					intent.putExtra("lottery_time", DataTimeTools.time2(mList.get(position).getLottery_time()));
					mContext.startActivity(intent);
				}
				
				
			}
		});

		// 查看物流
		get_logistics_info.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String kd_code = mList.get(position).getKd_code();
				String kd_key = mList.get(position).getKd_key();
				Intent intent = new Intent(mContext, LogisticsInfoActivity.class);
				intent.putExtra("kd_code", kd_code);
				intent.putExtra("kd_key", kd_key);
				intent.putExtra("uri", uri);
				mContext.startActivity(intent);
			}
		});
		
		item_get_goods_lay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				Toast.makeText(mContext, mList.get(position).getGoods_name() + "", Toast.LENGTH_SHORT).show();
//				Intent i = new Intent(mContext,ProductDetailActivity.class);
//				i.putExtra("goods_entity", mList.get(position));
//				mContext.startActivity(i);
			}
		});

		return convertView;
	}

	private void findview(View convertView) {
		get_goods_iv = ViewHolder.get(convertView, R.id.get_goods_iv);
		get_goods_tv = ViewHolder.get(convertView, R.id.get_goods_tv);
		get_price_tv = ViewHolder.get(convertView, R.id.get_price_tv);
		get_konw_times = ViewHolder.get(convertView, R.id.get_konw_times);
		get_orderId_tv = ViewHolder.get(convertView, R.id.get_orderId_tv);
		get_show_orderInfo = ViewHolder.get(convertView, R.id.get_show_orderInfo);
		get_logistics_info = ViewHolder.get(convertView, R.id.get_logistics_info);
		get_part_in_lay = ViewHolder.get(convertView, R.id.get_part_in_lay);
		item_get_goods_lay = ViewHolder.get(convertView, R.id.item_get_goods_lay);
	}

}
